//
//  YSOrderViewCtrl.m
//  Yueshi
//
//  Created by Northwan on 15/8/1.
//  Copyright (c) 2015年 悦食中国. All rights reserved.
//  订单

#import "YSOrderViewCtrl.h"
#import "JXScrollBar.h"
#import "YSOrdinaryOrderListCtrl.h"
#import "YSExperienceOrderListCtrl.h"

@interface YSOrderViewCtrl ()<UIScrollViewDelegate,JXScrollBarDelegate>

@property(nonatomic,strong)UIScrollView *scrollView;
@property(nonatomic,strong)NSMutableArray *childController;
@property(nonatomic,strong)NSMutableArray *titleArray;
@property(nonatomic,strong)JXScrollBar *scrollBar;

@end

@implementation YSOrderViewCtrl

-(BOOL)shouldLogin{
    return YES;
}

-(void)back{
    [self.navigationController dismissViewControllerAnimated:YES completion:nil];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _childController = [NSMutableArray array];
    _titleArray = [NSMutableArray array];
    [_titleArray addObject:@"美食订单"];
    [_titleArray addObject:@"体验券"];
    
    UIBarButtonItem *leftItem = [UIBarButtonItem itemWithImageName:@"nav_return_normal" highImageName:@"nav_return_normal" target:self action:@selector(back)];
    self.navigationItem.leftBarButtonItem = leftItem;
    
    UIScrollView *scrollView = [[UIScrollView alloc]init];
    scrollView.frame = CGRectMake(0, 0, SCREENW, SCREENH);
    scrollView.showsHorizontalScrollIndicator = NO;
    scrollView.pagingEnabled = YES;
    //设置scrollview的代理
    scrollView.delegate = self;
    scrollView.scrollsToTop = NO;
    scrollView.delaysContentTouches = NO;//这句代码是解决scrollview和tableview的滚动冲突
    _scrollView = scrollView;
    [self.view addSubview:scrollView];
    
    JXScrollBar *scrollBar = [[JXScrollBar alloc]initWithFrame:CGRectMake(-30, 0, SCREENW-16, 44) type:JXScrollBarSplitTypeCenter];
    scrollBar.delegate = self;
    _scrollBar = scrollBar;
    self.navigationItem.titleView = _scrollBar;
    
    [self inflateLayout];
}

-(void)tapTitleRightItem:(UIBarButtonItem *)btnItem{
    [Utils showCartCtrl:self];
}

-(void)inflateLayout{
    for (UIViewController *ctrl in _childController) {
        [ctrl.view removeFromSuperview];
    }
    [_childController removeAllObjects];
    
    //添加美食订单页
    YSOrdinaryOrderListCtrl *ordinaryCtrl = [[YSOrdinaryOrderListCtrl alloc]init];
    [_childController addObject:ordinaryCtrl];
    UIView *ordinaryView = ordinaryCtrl.view;
    ordinaryView.frame =CGRectMake(0, 0, SCREENW, SCREENH-NavWithStatusBarH);
    [_scrollView addSubview:ordinaryView];
    //添加体验券页
    YSExperienceOrderListCtrl *experienceCtrl = [[YSExperienceOrderListCtrl alloc]init];
    [_childController addObject:experienceCtrl];
    UIView *experienceView = experienceCtrl.view;
    experienceView.frame =CGRectMake(SCREENW, 0, SCREENW, SCREENH-NavWithStatusBarH);
    [_scrollView addSubview:experienceView];
    
    //设置scrollBar的数据
    [_scrollBar setTitleArray:_titleArray];
    [_scrollBar scrollViewDidScroll:_scrollView];
    //使第1页去加载数据
    [self scrollViewDidEndDecelerating:_scrollView];
    _scrollView.contentSize = CGSizeMake(_titleArray.count*SCREENW, 0);
    
    if([DefaultsHelper boolForKey:kOrderType]){// 如果是体验券订单，自动滚动至
        [self setScrollCurrentPageNum:1];
        [DefaultsHelper setBool:NO forKey:kOrderType];
    }
}
#pragma mark -jxscrollbar代理，处理点击title的button事件
- (void)titleClicked:(JXScrollBar *)scrollbar index:(int)index{
    [scrollbar autoScrollToCenter:index];
    [self setScrollCurrentPageNum:index];
}

#pragma mark -滚动代理
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    [_scrollBar scrollViewDidScroll:scrollView];
}

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    //当前页数
    int pageNum = (scrollView.contentOffset.x+scrollView.width*0.5)/scrollView.width;
    
    [self setScrollCurrentPageNum:pageNum];
    [_scrollBar scrollViewDidEndDecelerating:scrollView];
}

-(void)setScrollCurrentPageNum:(int)pageNum{
    CGPoint p = CGPointMake(pageNum*_scrollView.frame.size.width, 0);
    [_scrollView setContentOffset:p];
    //这句代码的作用是解决点击顶部之后底部的scrollview往上跑的问题
    _scrollView.y = 0;
    
    /*if ([_childController[pageNum] isKindOfClass:[YSOrdinaryOrderListCtrl class]]) {
                YSRecommendCtrl *controller = _childController[pageNum];
        //调用请求数据的方法
                [controller requestData];
    }*/
}

@end
